<template>
    <div>
        <!-- 条件查询 -->
        <el-card class="box-card">
            <el-form :inline="true" :model="query" size="mini">
                <el-form-item label="ID">
                    <el-input v-model.trim="query.id" placeholder="请输入ID" clearable></el-input>
                </el-form-item>
                <el-form-item label="作者">
                    <el-input v-model.trim="query.username" placeholder="请输入用户名" clearable></el-input>
                </el-form-item>
                <el-form-item label="标题">
                    <el-input v-model.trim="query.title" placeholder="请输入标题" clearable></el-input>
                </el-form-item>

                <el-form-item label="分类ID">
                    <el-input v-model.trim="query.categoryId" placeholder="请输入分类ID" clearable></el-input>
                </el-form-item>
                <el-form-item label="标签">
                    <el-input v-model.trim="query.label" placeholder="请输入标签" clearable></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="query.state" clearable style="width: 120px">
                        <!-- 0表示删除、1表示发布、2表示草稿、3表示置顶   -->
                        <el-option label="已删除" :value="0"></el-option>
                        <el-option label="已发布" :value="1"></el-option>
                        <el-option label="草稿" :value="2"></el-option>
                        <el-option label="置顶" :value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="日期范围">
                    <el-date-picker
                            clearable
                            v-model="query.begin"
                            placeholder="开始时间"
                            value-format="yyyy-MM-dd"/>
                </el-form-item>
                <el-form-item label="-">
                    <el-date-picker
                            clearable
                            v-model="query.end"
                            placeholder="结束时间"
                            value-format="yyyy-MM-dd"/>
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
                    <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
                    <el-button icon="el-icon-circle-plus-outline" type="success" @click="openAdd">发表文章
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <div>
            <el-table
                    v-loading="loading"
                    :data="list"
                    border
                    size="mini"
                    style="width: 100%">
                <el-table-column
                        fixed
                        prop="id"
                        label="ID"
                        align="center"
                        width="200">
                </el-table-column>
                <!--                <el-table-column-->
                <!--                        fixed-->
                <!--                        prop="userId"-->
                <!--                        label="作者ID"-->
                <!--                        align="center"-->
                <!--                        width="200">-->
                <!--                </el-table-column>-->
                <el-table-column
                        prop="title"
                        label="标题"
                        align="center"
                        width="250">
                </el-table-column>
                <!--                <el-table-column align="center" prop="cover" label="封面" width="60">-->
                <!--                    <template slot-scope="scope">-->
                <!--                        <el-image-->
                <!--                                class="img"-->
                <!--                                :src="scope.row.cover"-->
                <!--                                :preview-src-list="[scope.row.cover]">-->
                <!--                        </el-image>-->
                <!--                    </template>-->
                <!--                </el-table-column>-->
                <!--                <el-table-column align="center" prop="userAvatar" label="作者头像" width="60">-->
                <!--                    <template slot-scope="scope">-->
                <!--                        <el-image-->
                <!--                                class="img"-->
                <!--                                :src="scope.row.userAvatar"-->
                <!--                                :preview-src-list="[scope.row.userAvatar]">-->
                <!--                        </el-image>-->
                <!--                    </template>-->
                <!--                </el-table-column>-->
                <el-table-column
                        prop="username"
                        label="作者"
                        align="center"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="summary"
                        label="摘要"
                        align="center"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="categoryId"
                        label="分类ID"
                        align="center"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="label"
                        label="标签"
                        align="center"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="type"
                        label="类型"
                        align="center"
                        width="120">
                    <template slot-scope="scope">
                        <!-- 0表示富文本，1表示 markdowm -->
                        <el-tag type="danger" v-if="scope.row.type === '0'">富文本</el-tag>
                        <el-tag type="danger" v-if="scope.row.type === '1'">markdowm</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="state"
                        label="状态"
                        align="center"
                        width="120">
                    <template slot-scope="scope">
                        <!-- 0表示删除、1表示发布、2表示草稿、3表示置顶   -->
                        <el-tag v-if="scope.row.state === '0'">已删除</el-tag>
                        <el-tag type="success" v-if="scope.row.state === '1'">已发布</el-tag>
                        <el-tag v-if="scope.row.state === '2'">草稿</el-tag>
                        <el-tag type="danger" v-if="scope.row.state === '3'">置顶</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建时间"
                        align="center"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        label="更新时间"
                        align="center"
                        width="180">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        align="center"
                        width="200">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="handleEdit(scope.row)" size="small">重置密码</el-button>
                        <el-button type="danger" v-if="scope.row.state !== '0'" @click="handleDelete(scope.row.id)"
                                   size="small">删除
                        </el-button>
                        <el-button type="danger" v-if="scope.row.state === '0'" disabled
                                   @click="handleDelete(scope.row.id)" size="small">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-pagination
                    align="center"
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="page.current"
                    :page-sizes="[10, 20,30,50]"
                    :page-size="page.size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="page.total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
    import {getArticleList} from '@/api/api'

    export default {

        data() {
            return {
                loading: false,
                list: [],
                query: {},
                page: {
                    page: 1,
                    size: 10,
                    total: 0
                }
            }
        },
        created() {
            this.fetchData()
        },
        methods: {
            // 获取分类列表数据
            fetchData() {
                this.loading = true
                getArticleList(this.page.current, this.page.size, this.query).then(result => {
                    this.loading = false
                    if (result.code === 20000) {
                        this.list = result.data.records
                        this.page.total = result.data.total
                    }
                })
            },
            // 分页-切换每页显示多少条记录
            handleSizeChange(size) {
                this.page.size = size
                this.fetchData()
            },
            // 分页-切换当前页码
            handleCurrentChange(current) {
                this.page.current = current
                this.fetchData()
            },
            // 条件查询
            queryData() {
                this.page.current = 1
                this.fetchData()
            },
            // 条件查询-重置表单
            reload() {
                this.query = {}
                this.fetchData()
            },
            // 打开新增窗口
            openAdd() {
                this.$router.push({path:'/content/add'})
            },
            // 打开编辑窗口
            handleEdit(id) {
                console.log(id)
            },
            // 删除
            handleDelete() {

            }
        }
    }
</script>
<style scoped>
</style>